package cn.schoolwow.quickdao.module.database.dql.response.flow.common.resultSet;

import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

public class SetResultSetPropertyListFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ResultSet resultSet = (ResultSet) flowContext.checkData("resultSet");

        ResultSetMetaData metaData = resultSet.getMetaData();
        Property[] resultSetPropertyArray = new Property[metaData.getColumnCount()];
        for (int i = 1; i <= resultSetPropertyArray.length; i++) {
            resultSetPropertyArray[i - 1] = new Property();
            resultSetPropertyArray[i - 1].columnLabel = metaData.getColumnLabel(i);
            resultSetPropertyArray[i - 1].column = metaData.getColumnName(i);
            resultSetPropertyArray[i - 1].columnType = metaData.getColumnTypeName(i);
            resultSetPropertyArray[i - 1].className = metaData.getColumnClassName(i);
            resultSetPropertyArray[i - 1].entity = new Entity();
            resultSetPropertyArray[i - 1].entity.tableName = metaData.getTableName(i);
        }
        flowContext.putTemporaryData("resultSetPropertyArray", resultSetPropertyArray);
    }

    @Override
    public String name() {
        return "设置结果集属性列表";
    }
}
